#include <stdio.h>

/**
* MooseArray.h
* A template wrapper for a dynamic array
* @date July 18, 2010
*/

#ifndef _MooseArray
#define _MooseArray

#include <iostream>

template <class ITEM>

// Based on array class from "Data structures and Algorithms for Game Developers" - Allen Sherrod
class MooseArray
{
  private:
    ITEM* templArray;
    int size;

  public:
    MooseArray()
    {
      templArray = NULL;
      size = 0;
    }

    MooseArray( int arraySize )
    {
      size = arraySize;

      if ( size )
      {
        templArray = new ITEM[size];
      }
    }

    ~MooseArray()
    {
      if ( templArray != NULL )
      {
        delete [] templArray;
        templArray = NULL;
      }
    }

    ITEM* GetArray()
    {
      return templArray;
    }

    virtual ITEM& operator[]( int index )
    {
      return templArray[index];
    }

    virtual void Push( ITEM value )
    {
      Expand();
      templArray[ size-1 ] = value;
    }

    bool Empty()
    {
        return ( size <= 0 );
    }

    ITEM Top()
    {
        return templArray[size-1];
    }

    void Pop()
    {
      if ( size > 0 )
        size--;
    }

    ITEM TopAndPop()
    {
        if ( size > 0 )
            size--;
        return templArray[size];
    }

    // wtf?
    void Remove( int index )
    {
      if ( index > size )
        return;

      for( int i=index; i<size-1; i++ )
      {
        templArray[i] = templArray[i+1];
      }

      size--;
    }

    void Clear()
    {
      size = 0;
    }

    // Make array bigger
    bool Expand()
    {
      if ( templArray == NULL )
      {
        size = 1;
        templArray = new ITEM[size];
      }
      else
      {
        ITEM* temp = new ITEM[size+1];

        for( int i=0; i<size; i++ )
        {
          temp[i] = templArray[i];
        }

        delete [] templArray;
        templArray = NULL;

        templArray = new ITEM[ size+1 ];
        templArray = temp;

        temp = NULL;
        size++;
      }

      return true;
    }

    int Size()
    {
      return size;
    }
};

#endif

